@include('admin.header')
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="/admin/welcome">首页</a>
        <a href="">管理员管理</a>
        <a>
          <cite>管理员列表</cite></a>
      </span>
      <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:38px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input class="layui-input" autocomplete="off" placeholder="开始日" name="start" id="start">
          <input class="layui-input" autocomplete="off" placeholder="截止日" name="end" id="end">
          <input type="text" name="admin_name"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock>
        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('添加用户','/admin/admin_add')"><i class="layui-icon"></i>添加</button>
        <span class="x-right count" style="line-height:40px"></span>
      </xblock>
      <div class="list">




      </div>
      <div class="alert">
        提示信息
      </div>
      <style>
        .alert{
          position: fixed;
          width: 280px;
          height: 50px;
          top: 50%;
          left: 50%;
          transform: translate(-50%,-50%);
          z-index: 9999;
          color: #fff;
          text-align: center;
          line-height: 50px;
          font-size: 16px;
          display: none;
        }
      </style>
    <script>
      layui.use('laydate', function(){
        var laydate = layui.laydate;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
      });
      layui.use('form',function () {
        var form = layui.form;
        form.on('submit(sreach)', function(data){
          //getGoodData(page);
          var admin = data.field;
          var str = '';
          for(let k in admin){
            str+='&'+k+'='+admin[k];
          }
          $.ajax({
            type:"get",
            url:"{{url('/admin/toget_admin_list')}}",
            data:"page=1"+str,
            dataType:"html",
            success:function(res){
              $(".list").html(res);
              resetpage();
              $.ajax({
                type:"get",
                url:"{{url('/admin/toget_admin_count')}}",
                data:"page=1"+str,
                dataType:"text",
                success:function(res){
                  $(".count").html("共有管理员："+res+" 人");
                }
              })
            }
          })
          return false;
        })
        //搜索功能
        form.on("submit(sreach)",function (data) {
          console.log(data.field);
          var data_obj = data.field;
          if (data_obj["start"]!="") {
            start = "&start="+data_obj["start"];
          }else{
            start = "";
          }
          if (data_obj["end"]!="") {
            end = "&end="+data_obj["end"];
          }else{
            end = "";
          }

          if (data_obj["admin_name"]!="") {
            adminName = "&admin_name="+data_obj["admin_name"];
          }else{
            adminName = "";
          }
          var getstr = searchItem();
          $.ajax({
            type:"get",
            url:"{{url('/admin/toget_admin_list')}}",
            data:"page=1"+getstr,
            dataType:"html",
            success:function(res){
              $(".list").html(res);
              resetpage();
              getCount(getstr);
              form.render();
            }
          })
          return false;
        })
      })

       /*用户-停用*/
      function member_stop(obj,id){
          layer.confirm('确认要停用吗？',function(index){

              if($(obj).attr('title')=='启用'){

                //发异步把用户状态进行更改
                $(obj).attr('title','停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!',{icon: 5,time:1000});

              }else{
                $(obj).attr('title','启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                layer.msg('已启用!',{icon: 5,time:1000});
              }
              
          });
      }

      /*用户-删除*/
      function member_del(obj,id){
          layer.confirm('确认要删除吗？',function(index){
              //发异步删除数据
              //$(obj).parents("tr").remove();
              //layer.msg('已删除!',{icon:1,time:1000});
            $.ajax({
              type:"get",
              url:"{{url('/admin/todo_admin_del')}}",
              data:"admin_id="+id,
              dataType:"json",
              success:function(res){
                console.log(res);
                if(res.code==1){
                  $(obj).parents("tr").remove();
                  layer.msg('已删除!',{icon:1,time:1000});
                  getGoodData(1);
                  getCount();
                }else {
                  layer.msg('数据执行有误!',{icon:1,time:1000});
                }
              }
            })
          });
      }
      var adminName = "";
      var classify_id = "";
      var start = "";
      var end = "";
      function searchItem(){
        var str = "";
        if (adminName!="") {
          str += adminName;
        }
        if (classify_id!="") {
          str += classify_id;
        }
        if (start!="") {
          str += start;
        }
        if (end!="") {
          str += end;
        }
        return str;
      }


      function delAll (argument) {

        var data = tableCheck.getData();
  
        layer.confirm('确认要删除吗？'+data,function(index){
            //捉到所有被选中的，发异步进行删除
          var a = $("input[name='id[]']:checked");
          var arr =[];
          for(let i = 0;i<a.length;i++){
            arr.push(a[i].value);
          }



          console.log(arr);
          $.ajax({
            type:"post",
            url:"{{url('/admin/todo_admin_delall')}}",
            data:{"arr":arr},
            dataType:"json",
            success:function(res){
              if(res.code==1){
                layer.msg('已删除!',{icon:1,time:1000});
                getGoodData(1);
                getCount();
              }else {
                layer.msg('数据执行有误!',{icon:1,time:1000});
              }
            }
          })
        });
      }
      
    </script>

    <script type="text/javascript">
      //全选功能
      $(document).on("change","input[name='all']",function () {
        var flag = $("input[name='all']").get(0).checked;
        var arr = $("input[name='id[]']");
        for (let i = 0; i < arr.length; i++) {
          arr.get(i).checked = flag;
        }
      })
      //重置分页的样式
      function resetpage(){
        //把span标签改成a标签
        var spans = $('.page-item span');
        for (let i = 0; i < spans.length; i++) {
          spans[i].outerHTML="<a href=''>"+spans[i].innerHTML+"</a>";
        }
        var arr = $(".page-item a");
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].innerHTML=="‹") {
            arr[i].className = "prev";
          }else if(arr[i].innerHTML=="›"){
            arr[i].className = "next";
          }
        }
        //将当前页设置为active
        $(".active a").attr("class","num active");
        //把li 上面的active 去除
        $("li.active").removeClass("active");
        //给页码a标签换类名
        $(".page-item .page-link").attr("class","num");
        }
        //请求接口获取某一页的数据
        function getGoodData(page,getstr=""){
          $.ajax({
            type:"get",
            url:"{{url('/admin/toget_admin_list')}}",
            data:"page="+page+getstr,
            dataType:"html",
            success:function(res){
              $(".list").html(res);

              resetpage();
            }
          })
        }
      //请求接口获取所有数据的条数
      function getCount(getstr=""){
        $.ajax({
          type:"get",
          url:"{{url('/admin/toget_admin_count')}}",
          data:"p=1"+getstr,
          dataType:"text",
          success:function(res){
            $(".count").html("共有管理员："+res+" 人");
          }
        })
      }
        //页面加载完要先请求第一页和第一次请求数据总数
        getGoodData(1);
        getCount();

      //点击页码请求接口
      $(document).on("click",".num",function () {
        var page = $(this).html();
        let condition = searchItem();
        getGoodData(page,condition);
        return false;
      })
      //点击上一页请求接口
      $(document).on("click",".prev",function () {
        var isdisable = $(this).parent(".disabled").length;
        var cur = $(".page-item").find(".active").html();
        if (isdisable) {
          $(".alert").html("这是梦开始的地方").css({"background-color":"#41b314bb","border-color":"#3ca512bb"}).stop().fadeIn(500);
          setTimeout(function () {
            $(".alert").fadeOut(500);
          },2000)
        }else{
          var page = parseInt(cur)-1
          let condition = searchItem();
          getGoodData(page,condition);
        }
        return false;
      })
      //点击下一页请求接口
      $(document).on("click",".next",function () {
        var isdisable = $(this).parent(".disabled").length;
        var cur = $(".page-item").find(".active").html();
        if (isdisable) {
          $(".alert").html("这里的故事等你来续写").css({"background-color":"#41b314bb","border-color":"#3ca512bb"}).stop().fadeIn(500);
          setTimeout(function () {
            $(".alert").fadeOut(500);
          },2000)
        }else{
          var page = parseInt(cur)+1
          let condition = searchItem();
          getGoodData(page,condition);
        }
        return false;
      })
    </script>
  </body>

</html>